@extends('admin::layouts.master')
@section('content')
    <div class="col-lg-12">
        <div class="card card-table">
            <div class="card-header">用户管理
                <button data-toggle="modal" data-target="#add" type="button" class="btn btn-primary">添加用户</button>
                @component('componets.modal',['title'=>'添加用户','url'=>'/admin/adminuser','id'=>'add'])
                    <div class="form-group pt-2 text-left">
                        <label for="name">用户名</label>
                        <input id="name" required name="name" type="text" placeholder="请输入用户名" class="form-control" value="">
                    </div>
                    <div class="form-group pt-2 text-left">
                        <label for="mc">用户昵称</label>
                        <input id="mc" required name="nickname" type="text" placeholder="请输入用户昵称" class="form-control" value="">
                    </div>
                    <div class="form-group text-left">
                        <label for="mm">用户密码</label>
                        <input id="mm" required name="password" type="password" placeholder="请输入密码，如不更改请勿填写！" class="form-control">
                    </div>
                @endcomponent
            </div>
            <div class="card-body">
                <table class="table">
                    <thead>
                    <tr>
                        <th style="width:10%;">用户ID</th>
                        <th style="width:10%;">用户名</th>
                        <th>用户昵称</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($admins as $admin)
                    <tr id="tr{{$admin['id']}}">
                        <td>{!! $admin['id'] !!}</td>
                        <td>{!! $admin['name'] !!}</td>
                        <td>{!! $admin['nickname'] !!}</td>
                        <td>{!! $admin['created_at'] !!}</td>
                        <td class="actions">
                            <button data-toggle="modal" data-target="#edit{{$admin['id']}}" type="button" class="btn btn-primary">编辑</button>
                            <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal{{$admin['id']}}">
                                删除
                            </button>
                            <a href="/admin/showrole/{{$admin['id']}}" class="btn btn-primary">角色设置</a>
                            @component('componets.modal',['title'=>'编辑用户','url'=>'/admin/adminuser/'.$admin['id'],'id'=>'edit'.$admin['id'],'method'=>'PUT'])
                                <div class="form-group pt-2 text-left">
                                    <label for="username">用户名</label>
                                    <input name="name" type="text" placeholder="请输入用户名" required class="form-control" value="{{$admin['name']}}">
                                </div>
                                <div class="form-group pt-2 text-left">
                                    <label for="nickname">用户昵称</label>
                                    <input required name="nickname" type="text" placeholder="请输入用户昵称" class="form-control" value="{{$admin['nickname']}}">
                                </div>
                                <div class="form-group text-left">
                                    <label for="inputPassword">用户密码</label>
                                    <input name="password" type="password" placeholder="请输入密码，如不更改请勿填写！" class="form-control">
                                </div>
                            @endcomponent
                        </td>
                    </tr>
                    <div class="modal fade" id="exampleModal{{$admin['id']}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="exampleModalLabel">提示信息</h5>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    是否删除？删除后不可恢复，请谨慎操作
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                                    <button type="button" onclick="del({{$admin['id']}})" class="btn btn-primary">确定</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
@section('scripts')
    <script>
        function del(k){
            $.ajax({
                url:"/admin/adminuser/del/"+k,
                dataType:'json',
                success:function(res){
                    if(res.error==0){
                        $('#tr'+k).remove();
                        alert(res.msg);
                    }else{
                        alert(res.msg);
                    }
                }
            });
            $('#exampleModal'+k).modal('hide');
        }
    </script>

@endsection
